#!/bin/bash
# vim: set ft=sh

set -e

RUN_DIR=/var/vcap/sys/run/blackbox
LOG_DIR=/var/vcap/sys/log/blackbox
PIDFILE=$RUN_DIR/blackbox.pid

source /var/vcap/packages/pid_utils/pid_utils.sh

case $1 in

  start)
    pid_guard $PIDFILE "blackbox"

    mkdir -p $RUN_DIR
    chown -R vcap:vcap $RUN_DIR

    mkdir -p $LOG_DIR
    chown -R vcap:vcap $LOG_DIR

    echo $$ > $PIDFILE

    exec chpst -u vcap:vcap /var/vcap/packages/blackbox/bin/blackbox \
      -config=/var/vcap/jobs/blackbox/config/config.yml \
      1>>$LOG_DIR/blackbox.stdout.log \
      2>>$LOG_DIR/blackbox.stderr.log

    ;;

  stop)
    kill_and_wait $PIDFILE

    ;;

  *)
    echo "Usage: $0 {start|stop}"

    ;;

esac
